package controllers

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	httpfacade "github.com/hulutech-web/http_result"
	"goravel/app/http/requests"
	"goravel/app/models"
)

type ParkingController struct {
	//Dependent services
}

func NewParkingController() *ParkingController {
	return &ParkingController{
		//Inject services
	}
}

// Index 用户分页查询，支持搜索，路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ParkingController
// @Accept       json
// @Produce      json
// @Id ParkingIndex
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param  name  query  string  false  "name"
// @Param  pageSize  query  string  false  "pageSize"
// @Param  currentPage  query  string  false  "currentPage"
// @Param  sort  query  string  false  "sort"
// @Param  order  query  string  false  "order"
// @Success 200 {string} json {}
// @Router       /api/admin/parking [get]
func (r *ParkingController) Index(ctx http.Context) http.Response {
	parkings := []models.Parking{}
	queries := ctx.Request().Queries()
	res, err := httpfacade.NewResult(ctx).SearchByParams(queries, nil).ResultPagination(&parkings)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "", err.Error())
	}
	return res
}

func (r *ParkingController) Show(ctx http.Context) http.Response {

	return nil
}

// Store 用户分页查询，支持搜索，路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ParkingController
// @Accept       json
// @Produce      json
// @Id ParkingStore
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param carData body requests.ParkingRequest true "用户数据"
// @Success 200 {string} json {}
// @Router       /api/admin/parking [post]
func (r *ParkingController) Store(ctx http.Context) http.Response {
	var request requests.ParkingRequest
	errors, err := ctx.Request().ValidateRequest(&request)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据错误", err.Error())
	}
	if errors != nil {
		return httpfacade.NewResult(ctx).ValidError("", errors.All())
	}

	facades.Orm().Query().Model(&models.Parking{}).Create(&models.Parking{
		ParkingType: request.ParkingType,
		No:          request.No,
	})
	return httpfacade.NewResult(ctx).Success("创建成功", nil)
}

// Update 用户分页查询，支持搜索，路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ParkingController
// @Accept       json
// @Produce      json
// @Id ParkingUpdate
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param carData body requests.ParkingRequest true "用户数据"
// @Success 200 {string} json {}
// @Router       /api/admin/parking/{id} [put]
func (r *ParkingController) Update(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	pk := models.Parking{}
	facades.Orm().Query().Model(&models.Parking{}).Where("id=?", id).Find(&pk)
	var request requests.ParkingRequest
	errors, err := ctx.Request().ValidateRequest(&request)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据错误", err.Error())
	}
	if errors != nil {
		return httpfacade.NewResult(ctx).ValidError("", errors.All())
	}
	facades.Orm().Query().Model(&models.Parking{}).Where("id=?", id).Update(&models.Parking{
		ParkingType: request.ParkingType,
		No:          request.No,
	})
	return httpfacade.NewResult(ctx).Success("修改成功", nil)
}

// Destroy 查询工时标准
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ParkingController
// @Accept       json
// @Produce      json
// @Id ParkingDestroy
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/admin/parking/{id} [delete]
func (r *ParkingController) Destroy(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	facades.Orm().Query().Model(&models.Parking{}).Delete(&models.Parking{}, id)
	return httpfacade.NewResult(ctx).Success("删除成功", nil)
}

// Option 用户分页查询，支持搜索，路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
// @Tags         ParkingController
// @Accept       json
// @Produce      json
// @Id ParkingOption
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/admin/parking/option [get]
func (r *ParkingController) Option(ctx http.Context) http.Response {
	parkings := []models.Parking{}
	facades.Orm().Query().Model(&models.Parking{}).Where("state=?", "空闲").Find(&parkings)
	return httpfacade.NewResult(ctx).Success("", parkings)
}

// Online 用户分页查询，支持搜索，路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
// @Tags         ParkingController
// @Accept       json
// @Produce      json
// @Id ParkingOnline
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/admin/parking/online [get]
func (r *ParkingController) Online(ctx http.Context) http.Response {
	parkings := []models.Parking{}
	_, err := httpfacade.NewResult(ctx).List(&parkings)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "", err.Error())
	}
	return httpfacade.NewResult(ctx).Success("", parkings)
}
